/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.intec.matsab.persistence;

/**
 *
 * @author Sabrina
 */

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;

public class PersistenceManager {
    public static final boolean DEBUG = true;
    private static final PersistenceManager singleton = new PersistenceManager();
    private static Logger logger = Logger.getLogger(PersistenceManager.class);
    protected EntityManagerFactory emf;

    public static PersistenceManager getInstance() {
        return singleton;
    }

    private PersistenceManager() {
    }

    public EntityManagerFactory getEntityManagerFactory() {
        if (emf == null) {
            createEntityManagerFactory();
        }

        return emf;
    }

    public void closeEntityManagerFactory() {
        if (emf != null) {
            emf.close();
            emf = null;

            if (DEBUG) {
                logger.info("*** Persistence finished at " + new java.util.Date());
            }
        }
    }

    protected void createEntityManagerFactory() {
        this.emf = Persistence.createEntityManagerFactory("matsabPU");

        if (DEBUG) {
            logger.info("*** Persistence started at " + new java.util.Date());
        }
    }
}
